package com.softeem.music_sys.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.softeem.music_sys.comm.Result;
import com.softeem.music_sys.entity.User;
import com.softeem.music_sys.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "用户接口", description = "用户相关操作：注册、登录、信息修改等")
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    // 用户注册
    @Operation(summary = "用户注册", description = "用户注册，提供JSON格式用户数据")
    @PostMapping("/register")
    public Result register(@RequestBody User user) {
        // 检查用户名是否已存在
        User existingUser = userService.getOne(new com.baomidou.mybatisplus.core.conditions.query.QueryWrapper<User>().eq("username", user.getUsername()));
        if (existingUser != null) {
            return Result.fail("用户名已存在");
        }
        // 保存用户
        boolean flag = userService.save(user);
        return flag ? Result.success().setMsg("注册成功").setData(user) : Result.fail("注册失败");
    }

    // 用户登录
    @Operation(summary = "用户登录", description = "用户登录，提供用户名和密码")
    @Parameters({
            @Parameter(name = "username", description = "用户名"),
            @Parameter(name = "password", description = "密码")
    })
    @GetMapping("/login")
    public Result login(String username, String password) {
        // 根据用户名查询用户信息
        User user = userService.getOne(new com.baomidou.mybatisplus.core.conditions.query.QueryWrapper<User>().eq("username", username));
        if (user == null) {
            return Result.fail("用户不存在");
        }
        if (!password.equals(user.getPassword())) {
            return Result.fail("密码错误");
        }
        if (user.getStatus() == 0) {
            return Result.fail("账号已被禁用");
        }
        // 清除密码信息，防止泄露
        user.setPassword(null);
        return Result.success().setMsg("登录成功").setData(user);
    }

    // 用户信息修改
    @Operation(summary = "用户信息修改", description = "用户信息修改，提供JSON格式用户数据")
    @PutMapping
    public Result update(@RequestBody User user) {
        boolean flag = userService.updateById(user);
        return flag ? Result.success().setMsg("修改成功") : Result.fail("修改失败");
    }

    // 删除用户
    @Operation(summary = "删除用户", description = "根据用户ID删除用户")
    @Parameters({
            @Parameter(name = "id", description = "用户ID")
    })
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        // 检查用户是否存在
        User user = userService.getById(id);
        if (user == null) {
            return Result.fail("用户不存在");
        }
        boolean flag = userService.removeById(id);
        return flag ? Result.success().setMsg("删除成功") : Result.fail("删除失败");
    }

    // 用户列表查询（分页）
    @Operation(summary = "用户列表查询", description = "用户列表查询，支持分页")
    @Parameters({
            @Parameter(name = "page", description = "页码"),
            @Parameter(name = "limit", description = "每页数量")
    })
    @GetMapping("/list")
    public Result list(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer limit
    ) {
        Page<User> pageUtil = new Page<>(page, limit);
        List<User> list = userService.list(pageUtil);
        return Result.success().setData(list);
    }

    // 根据ID获取用户信息
    @Operation(summary = "获取用户信息", description = "根据用户ID获取用户详细信息")
    @Parameters({
            @Parameter(name = "id", description = "用户ID")
    })
    @GetMapping("/{id}")
    public Result getUserById(@PathVariable Integer id) {
        User user = userService.getById(id);
        if (user == null) {
            return Result.fail("用户不存在");
        }
        // 清除密码信息
        user.setPassword(null);
        return Result.success().setData(user);
    }
}